package jaux.tank.service.system.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import jaux.tank.bean.entity.system.Dept;
import jaux.tank.bean.vo.node.DeptNode;
import jaux.tank.bean.vo.node.ZTreeNode;
import jaux.tank.dao.system.DeptRepository;
import jaux.tank.service.system.DeptService;

/**
 * Created  on 2018/3/21 0021.
 *
 */
@Service
public class DeptServiceImpl  implements DeptService {
	
    private final DeptRepository deptRepository;

    DeptServiceImpl(DeptRepository deptRepository) {
        this.deptRepository = deptRepository;
    }

    @Override
    public List<ZTreeNode> tree() {
    	List<ZTreeNode> nodes = new ArrayList<>();
        List<Dept> list = deptRepository.findAll();
        if(list!=null) {
        	nodes.addAll(list.stream().map(this::transfer).collect(Collectors.toList()));
        }
        return nodes;
    }

    @Override
    public List<ZTreeNode> tree(String deptId) {
    	if(StringUtils.isBlank(deptId)) {
    		return tree();
    	}
    	List<ZTreeNode> nodes = new ArrayList<>();
    	Optional<Dept> root = deptRepository.findById(deptId);
    	if(root.isPresent()) {
    		ZTreeNode rootNode = transfer(root.get());
    		rootNode.setpId("0");
			nodes.add(rootNode);
    	}
    	List<Dept> subDepts = querySubDepts(deptId);
    	for(Dept dept:subDepts) {
    		nodes.add(transfer(dept));
    	}
    	return nodes;
    }
    private ZTreeNode transfer(Dept dept){
        ZTreeNode node = new ZTreeNode();
        node.setId(dept.getId());
        Dept parentDept = dept.getParentDept();
		node.setpId(parentDept==null?"0":parentDept.getId());
        node.setName(dept.getSimplename());
        node.setIsOpen(parentDept==null || parentDept.getId()==null);
        return node;
    }
    @Override
    public List<Dept> query(String nameLikes) {
        List<Dept> list =null;
        if(StringUtils.isEmpty(nameLikes)){
             list = deptRepository.findAll();
        }else{
            nameLikes = "%"+nameLikes+"%";
             list = deptRepository.findBySimplenameLikeOrFullnameLike(nameLikes,nameLikes);
        }
        return list;
    }

    @Override
    public List<Dept> querySubDepts(String deptId) {
    	List<Dept> result= new ArrayList<>();
    	if(deptId !=null) {
    		result.addAll(deptRepository.findByParentDeptId(deptId));
    	}else {
    		result.addAll(deptRepository.findByParentDeptId(null));
    	}
    	if(CollectionUtils.isNotEmpty(result)) {
    		result.addAll(result.stream().map(p->querySubDepts(p.getId())).flatMap(deps->deps.stream()).collect(Collectors.toList()));
    	}
    	return result;
    }
    @Override
    public void deleteDept(String deptId) {
        deptRepository.findById(deptId).ifPresent(dept->{
        	
        	List<Dept> subDepts = deptRepository.findByParentDeptId("%[" + dept.getId() + "]%");
        	deptRepository.deleteAll(subDepts);
        	deptRepository.delete(dept);
        	
        });
    }

    @Override
    public List<DeptNode> queryAll() {
        List<Dept> list = deptRepository.findAll();

        return generateTree(list);
    }


    private List<DeptNode> generateTree(List<Dept> list){

        List<DeptNode> nodes = new ArrayList<>(20);
        for(Dept dept:list){
            DeptNode deptNode = new DeptNode();
            BeanUtils.copyProperties(dept,deptNode);
            nodes.add(deptNode);
        }
        for(DeptNode deptNode:nodes){
            for(DeptNode child:nodes){
                if(deptNode.getId().equals(child.getPid())){
                    deptNode.getChildren().add(child);
                }
            }
        }
        List<DeptNode> result = new ArrayList<>(20);
        for(DeptNode node:nodes){
            if(node.getPid() == null){
                result.add(node);
            }
        }
        return result;


    }


}
